package schoolsource.model;

//import java.lang.Integer;
//import java.lang.Double;
//import java.lang.Boolean;
//import java.lang.NumberFormatException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for homework.
 */
public class HomeworkContainer extends Container {

	// The map of timer buckets for all databases
	protected static Hashtable dbStores = new Hashtable();
	// The auto-generated id of the homework
	private IntegerItem homeworkID =
		new IntegerItem(SchoolDBSQL.HWASSIGNEDID, "", true);
	// The auto-generated ID of the class in which this homework was assigned.
	private StringItem classID =
		new StringItem(SchoolDBSQL.CLASSASSIGNEDID, "", true);
	// A description of the homework.
	private StringItem description =
		new StringItem(SchoolDBSQL.HWDESCRIPTION, "", true);
	// The total points possible for the homework.
	private IntegerItem totalPoints =
		new IntegerItem(SchoolDBSQL.HWTOTALPOINTS, "", true);
	// The date on which the homework was assigned.
	private DateNoTimeItem dateAssigned =
		new DateNoTimeItem(SchoolDBSQL.HWDATEASSIGNED, "", false);
	// The date and time on which the homework was last udpated
	private DateItem dateUpdated =
		new DateItem(SchoolDBSQL.HWDATEUPDATED, "", false);
	// The date on which the homework is due.
	private DateNoTimeItem dateDue =
		new DateNoTimeItem(SchoolDBSQL.HWDATEDUE, "", false);
	// A short name for the homework.
	private StringItem shortName =
		new StringItem(SchoolDBSQL.HWSHORTNAME, "", true);
	// Answers to the homework.
	private StringItem answers =
		new StringItem(SchoolDBSQL.HWANSWERS, "", true);
	// Notes or hints regarding the homework.
	private StringItem notes = new StringItem(SchoolDBSQL.HWNOTES, "", true);

    private BooleanItem submissionsAllowed =
        new BooleanItem(SchoolDBSQL.HWSUBMISSIONSALLOWED, "FALSE", true);
	// A list of attachments to the homework.
	private ArrayList attachments = new ArrayList();

	/**
	 * Default constructor.
	 */
	private HomeworkContainer() {

	}

	/**
	 * Constructor that returns a new instance of the container pre-populated with
	 * values loaded from the database based on the homework ID and class ID passed
	 * in as parameters.
	 * @param hID The auto-generated ID of the homework.
	 * @param cID The auto-generated ID of the class in which the homework was assigned.
	 */
//	public HomeworkContainer(String hID, String cID) {
	private HomeworkContainer(String hID, String dbName) {
		getHomeworkID().setValue(hID);
		//		homeworkID = new IntegerItem(SchoolDBSQL.HWASSIGNEDID, hID, true);
//		getClassID().setValue(cID);
		//		classID = new StringItem(SchoolDBSQL.CLASSASSIGNEDID, cID, true);
		SchoolDBSQL.getSchoolDBSQL(dbName).loadHomework(
			this);
	}

	public static HomeworkContainer getHomeworkContainer(String hID, String dbName) {
		HomeworkContainer returnVal = null;
		DBStore dbs = null;

		if (hID.equals("-1")) {
			returnVal = new HomeworkContainer();
		}
		else {
			if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
				dbs = new DBStore();
				dbStores.put(dbName, dbs);
				dbs.start();
			}
			if ((returnVal = (HomeworkContainer) dbs.getContainer(hID)) == null) {
				returnVal = new HomeworkContainer();
				returnVal.getHomeworkID().setValue(hID);
				SchoolDBSQL.getSchoolDBSQL(dbName).loadHomework(returnVal);
				dbs.addContainer(returnVal, hID);
			}
		}

		return returnVal;
	}

	/* (non-Javadoc)
	 * @see schoolsource.model.Container#getValidationItems()
	 */
	public Item[] getValidationItems() {
		ArrayList al = new ArrayList();
		al.add(homeworkID);
		al.add(classID);
		al.add(description);
		al.add(totalPoints);
		al.add(dateAssigned);
		al.add(dateDue);
		al.add(shortName);
		al.add(answers);
		al.add(notes);
		al.add(dateUpdated);
        al.add(submissionsAllowed);

		return (Item[]) al.toArray(new Item[al.size()]);
	}

	/**
	 * Getter for the homework ID.
	 * @return The auto-generated ID of the homework.
	 */
	public IntegerItem getHomeworkID() {
		return homeworkID;
	}

	/**
	 * Getter for the class ID in which this homework was assigned.
	 * @return The auto-generated ID of the class.
	 */
	public StringItem getClassID() {
		return classID;
	}

	/**
	 * Getter for the description of the homework.
	 * @return A description of the homework.
	 */
	public StringItem getDescription() {
		return description;
	}

	/**
	 * Getter for the total points the homework is worth.
	 * @return The total points the homework is worth.
	 */
	public IntegerItem getTotalPoints() {
		return totalPoints;
	}

	/**
	 * Getter for the date on which the homework was assigned.
	 * @return The date the homework was assigned.
	 */
	public DateNoTimeItem getDateAssigned() {
		return dateAssigned;
	}

	/**
	 * Getter for the date on which the homework is due.
	 * @return The date the homework is due.
	 */
	public DateNoTimeItem getDateDue() {
		return dateDue;
	}

	/**
	 * Getter for the date on which the homework is due.
	 * @return The date the homework is due.
	 */
	public DateItem getDateUpdated() {
		return dateUpdated;
	}

	/**
	 * Getter for the short name of the homework, such as 'Assignment 1'.
	 * @return The short name of the homework.
	 */
	public StringItem getShortName() {
		return shortName;
	}

	/**
	 * Getter for the answers to the homework.
	 * @return The answers to the homework.
	 */
	public StringItem getAnswers() {
		return answers;
	}

	/**
	 * Getter for the notes regarding the homework.
	 * @return The notes or hints regarding the homework.
	 */
	public StringItem getNotes() {
		return notes;
	}

    public BooleanItem getSubmissionsAllowed() {
        return submissionsAllowed;
    }

	/**
	 * Associates an attachment to the homework.
	 * @param ac The attachment to associate with the homework.
	 */
	public void addAttachment(AttachmentContainer ac) {
		attachments.add(ac);
	}

	/**
	 * Removes an attachment from association with the homework.
	 * @param ac The attachment that was removed.
	 */
	public AttachmentContainer removeAttachment(String attachmentID) {
		AttachmentContainer returnVal = null;
		for (int i = 0; i < attachments.size(); i++) {
			if ((returnVal = (AttachmentContainer) attachments.get(i))
				.getAssignedAttachmentID()
				.toString()
				.equals(attachmentID)) {
				attachments.remove(i);
				break;
			} else {
				returnVal = null;
			}
		}
		return returnVal;
	}

	/**
	 * Returns an array of attachments associated with this homework.
	 * @return Attachments associated with this homework.
	 */
	public AttachmentContainer[] getAttachments() {
		return (AttachmentContainer[]) attachments.toArray(
			new AttachmentContainer[0]);
	}

	/**
	 * Prints the values of the container.
	 */
	public void printItems() {
		SchoolLogger.logger.log(
			Level.INFO,
			"************************************************");
		Item iList[] = getValidationItems();
		for (int j = 0; j < iList.length; j++) {
			if (iList[j] != null) {
				SchoolLogger.logger.log(
					Level.INFO,
					iList[j].getFieldName() + ": " + iList[j].toString());
			}
		}
	}
}
